package com.ihome.android.market2.asynctask;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;

import com.ihome.android.analytic.JsonAnalytic;
import com.ihome.android.http.HttpActions;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.util.exception.ITVException;
import com.ihome.android.util.log.LogUtil;

/**
 * 应用评分请求
 * 
 * @author Emerson
 * 
 */
public class AppRateTask extends AsyncTask<Bundle, Integer, Bundle> {
	private final String		TAG	= AppRateTask.class.getSimpleName();
	private Context				context;

	public AppRateTask(Context context) {
		this.context = context;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
	}

	@Override
	protected Bundle doInBackground(Bundle... params) {
		Bundle b = params[0];
		String appId = b.getString(HttpKeys.KEY_APPID);
		String apprate = b.getString(HttpKeys.KEY_APPRATE);
		String content = b.getString(HttpKeys.KEY_CONTENT);
		LogUtil.v(TAG, "appId = " + appId + "; apprate = " + apprate);
		if (TextUtils.isEmpty(appId)) {
			return null;
		}

		try {
			String rateinfo = new HttpActions(context).appEvaluate(appId, apprate, content);
			LogUtil.v(TAG, "app rate  = " + rateinfo);
			JsonAnalytic.getInstance().analyseSimpleResult(rateinfo);

		} catch (ITVException e) {
			e.printStackTrace();
			LogUtil.e(TAG, e.toString());
			b.putInt(HttpKeys.KEY_CODE, e.getStatusCode());
			b.putString(HttpKeys.KEY_DESCRIPTION, e.getMessage());
		}
		return b;
	}

	@Override
	protected void onPostExecute(Bundle result) {
		super.onPostExecute(result);

		/** 请求出错处理 */
//		if (result.containsKey(HttpKeys.KEY_CODE)) {
//			if ( context instanceof DetailActivity) {
//				((DetailActivity) context).appRateResult(false);
//			}
//		}else{
//			if ( context instanceof DetailActivity) {
//				((DetailActivity) context).appRateResult(true);
//			}
//		}

	}

	@Override
	protected void onCancelled() {
		super.onCancelled();
		LogUtil.i(TAG, "--> onCancelled");

	}

}
